package com.dream.tihai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dream.tihai.dto.TreeTypeDto;
import com.dream.tihai.entity.TypeEntity;
import com.dream.tihai.mapper.TypeMapper;
import com.dream.tihai.service.ITypeService;
import com.dream.tihai.struct.TypeStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Valley
 * Created by Valley on 2022/12/14
 */
@Service
public class TypeServiceImpl implements ITypeService {

    @Autowired
    TypeMapper typeMapper;

    @Override
    public List<TreeTypeDto> selectTreeNode() {
        // 1 获取所有数据
        List<TreeTypeDto> allList = selectTreeList();
        // 2 获取父亲列表
        List<TreeTypeDto> parentList = getParentList(allList);
        // 3 获取父亲下的所有儿子的父亲列表
        List<TreeTypeDto> childrenList = getChildrenList(allList, parentList);
        return childrenList;
    }

    @Override
    public long addChildNode(String pName, String nodeName) {
        TypeEntity entity = new TypeEntity();
        LambdaQueryWrapper<TypeEntity> type = new LambdaQueryWrapper<>();
        type.eq(TypeEntity::getName, pName);
        List<TypeEntity> list = typeMapper.selectList(type);
        long pid = list.get(0).getId();
        type.clear();
        type.eq(TypeEntity::getPid, pid);
        list = typeMapper.selectList(type);
        if (list == null || list.size() == 0){
            entity.setId(pid*100+1);
        }else {
            long max = list.stream().mapToLong(TypeEntity::getId).max().getAsLong();
            entity.setId(max+1);
        }
        entity.setPid(pid);
        entity.setName(nodeName);
        int insert = typeMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public boolean removeType(String name) {
        LambdaQueryWrapper<TypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeEntity::getName, name);
        long id = typeMapper.selectList(wrapper).get(0).getId();
        int i = typeMapper.deleteById(id);
        return false;
    }

    private List<TreeTypeDto> selectTreeList() {
        LambdaQueryWrapper<TypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeEntity::getFlag, 0);
        List<TypeEntity> list = this.typeMapper.selectList(wrapper);
        return TypeStruct.INSTANCE.toTeeType(list);
    }


    // 获取父亲列表
    private List<TreeTypeDto> getParentList(List<TreeTypeDto> allList){
        List<TreeTypeDto> parentList = new ArrayList<>();
        allList.forEach(emrModleVo -> {
            if(emrModleVo.getPid() == 0){
                parentList.add(emrModleVo);
            }
        });
        return parentList;
    }

    // 获取父亲下的所有儿子的父亲列表
    private List<TreeTypeDto> getChildrenList(List<TreeTypeDto> allList,List<TreeTypeDto> parentList){
        parentList.forEach(one -> {
            List<TreeTypeDto> childrenList = new ArrayList<>();
            allList.forEach(all -> {
                if(one.getValue() == all.getPid()){
                    childrenList.add(all);
                }
            });
            one.setChildren(getChildrenList(allList,childrenList));
        });
        return parentList;
    }
}
